package cn.crism.blog.controller;

import cn.crism.blog.entity.User;
import cn.crism.blog.param.RePwdParam;
import cn.crism.blog.service.UserService;
import cn.crism.blog.util.ResultVOUtil;
import cn.crism.blog.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

/**
 * @Author: Crism
 * @Date: 上午8:36 18/3/18
 * @Description:
 */
@RestController
@RequestMapping("/api")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/Login")
    public ResultVO Login(@RequestBody @Valid User user, HttpSession session){
        User u = userService.existUser(user);
        if (u!=null){
            session.setAttribute("u", u);
            return ResultVOUtil.success(u);
        }
        return ResultVOUtil.error(1,"登录失败！");
    }

    @PostMapping("/modifyPwd")
    public ResultVO modifyPwd(@RequestBody @Valid RePwdParam rePwdParam, HttpSession session) {
        User user =(User) session.getAttribute("u");
        return userService.modifyPwd(rePwdParam,user)? ResultVOUtil.success("修改成功！"):ResultVOUtil.error(1,"修改失败！");
    }
}
